package com.jinxiangyang.controller;


import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.Menu;
import com.jinxiangyang.mapper.MenuMapper;
import com.jinxiangyang.service.MenuService;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jinxiangyang
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/menu")
@Api(tags="菜单控制器")
public class MenuController {
   @Autowired
   private MenuService menuService;

   @GetMapping("/{id}")
   @OperateLog(title = "菜单模块",operatorType = OperatorType.ADMIN, businessType = BusinessType.OTHER)
   public AjaxResult getMenuId(@PathVariable Integer id){
      List<Menu> treeNode = menuService.getTreeNode(id);
      return AjaxResult.success(treeNode);
   }

   @PostMapping
   @ApiOperation("添加菜单")
   @OperateLog(title = "菜单模块",operatorType = OperatorType.ADMIN, businessType = BusinessType.INSERT)
   public AjaxResult saveMenu(@RequestBody Menu menu){
      if(menuService.save(menu)){
         return AjaxResult.success();
      }else{
         return AjaxResult.error("插入失败");
      }
   }

   @PutMapping
   @ApiOperation("更新菜单")
   @OperateLog(title = "菜单模块",operatorType = OperatorType.ADMIN, businessType = BusinessType.UPDATE)
   public AjaxResult updateMenu(@RequestBody Menu menu){
      if(menuService.updateById(menu)){
         return AjaxResult.success();
      }else{
         return AjaxResult.error("插入失败");
      }
   }

   @DeleteMapping("/{id}")
   @ApiOperation("删除菜单")
   @OperateLog(title = "菜单模块",operatorType = OperatorType.ADMIN, businessType = BusinessType.INSERT)
   public AjaxResult deleteMenu(@PathVariable Integer id){
      if(menuService.removeById(id)){
         return AjaxResult.success();
      }else{
         return AjaxResult.error("插入失败");
      }
   }
}

